package org.jeecg.modules.demo.flowable.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.flowable.engine.HistoryService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;

import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.jeecg.modules.demo.flowable.model.ProcessInstanceEntity;
import org.jeecg.modules.demo.flowable.model.ProcessStatusEntity;
import org.jeecg.modules.demo.flowable.model.TaskInstanceEntity;
import org.jeecg.modules.demo.flowable.service.IProcessMethods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProcessMethodsImpl implements IProcessMethods {
    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    @Override
    public Task checkTaskById(String id){
        List<Task> list = taskService.createTaskQuery()
                .active() //处于活动中的任务
                .list();// 返回多条记录
        for (Task task : list) {
            if (StrUtil.equals(task.getProcessInstanceId(),id)){
                return task;
            }
        }
        return null;
    }

    /**
     * 发起一个流程方法的封装
     */
    @Override
    public String initiateProcess(String bussinessKey,String assignee) {
        //发起流程
        Map<String, Object> personMap = new HashMap<>();
        //设置第一个活动的审批人
        personMap.put("zuanyuan",assignee);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(ProcessStatusEntity.processDefinitionKey, bussinessKey, personMap);
        //返回流程实例ID
        return processInstance.getId();
    }

    /**
     * 取消流程方法
     */
    @Override
    public void cancelProcess(String processInstanceId) {
        runtimeService.deleteProcessInstance(processInstanceId, "取消原因");
    }

    /**
     * 获取我的执行中任务
     */
    @Override
    public List<Task> getSomeoneOngoingTasks(String assignee) {
        return taskService.createTaskQuery()
                .taskAssignee(assignee) // 根据审批人来查询
                .active() //处于活动中的任务
                .list();// 返回多条记录
    }

    /**
     * 获取我已处理任务
     */
    @Override
    public List<HistoricTaskInstance> getSomeoneProcessingCompleted(String assignee) {
        //需要去除有原因的
        List<HistoricTaskInstance> historicTaskInstances = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(assignee)
                .finished()
                .list();
        List<HistoricTaskInstance> historicTaskInstances2 = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(historicTaskInstances)){
            for (HistoricTaskInstance historicTaskInstance : historicTaskInstances) {
                //只获取取消原因字段为空
                if (StrUtil.isEmpty(historicTaskInstance.getDeleteReason())){
                    historicTaskInstances2.add(historicTaskInstance);
                }
            }
        }
        return historicTaskInstances2;
    }

    /**
     * 获取我发起的流程
     */
    @Override
    public List<HistoricTaskInstance> getSomeoneProcessInitiated(String assignee) {
        return historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(assignee)
                .list();
    }

    /**
     * 获取我参与的流程
     */
    @Override
    public List<HistoricTaskInstance> getSomeoneParticipateProcess(String assignee) {
        return historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(assignee)
                .list();
    }

    /**
     *
     *  完成我的任务
     * @param assignee 当前审核人
     * @param procInstId 当前流程实例ID
     * @param nextZhiwei 下个活动的职位名称
     * @param nextAssignee 下个活动的审核人的ID
     */
    @Override
    public void completeSomeoneTask(String assignee,String procInstId,String nextZhiwei,String nextAssignee) {
        // 查询的其实就是 act_ru_task 中的记录
        List<Task> someoneOngoingTasks = getSomeoneOngoingTasks(assignee);
        if (CollectionUtil.isNotEmpty(someoneOngoingTasks)){
            for (Task task : someoneOngoingTasks) {
                //只审核当前报表的
                if (task.getProcessInstanceId().equals(procInstId)){
                    Map<String, Object> personMap = new HashMap<>();
                    personMap.put(nextZhiwei,nextAssignee);
                    taskService.complete(task.getId(),personMap);
                }

            }
        }

    }
    @Override
    public void completeSomeoneTask001(String assignee,String procInstId) {
        // 查询的其实就是 act_ru_task 中的记录
        List<Task> someoneOngoingTasks = getSomeoneOngoingTasks(assignee);
        if (CollectionUtil.isNotEmpty(someoneOngoingTasks)){
            for (Task task : someoneOngoingTasks) {
                //只审核当前报表的
                if (task.getProcessInstanceId().equals(procInstId)){
                    taskService.complete(task.getId());
                }
            }
        }
    }

    /**
     *  完成我的任务
     * @param assignee 当前审核人
     * @param procInstId 当前流程实例ID
     * @param nextZhiwei 下个活动的职位名称
     * @param nextAssignee 下个活动的审核人的ID
     * @param value 审核结果value
     */
    @Override
    public void completeSomeoneTask(String assignee,String procInstId,String nextZhiwei,String nextAssignee,boolean value) {
        // 查询的其实就是 act_ru_task 中的记录
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(assignee) // 根据审批人来查询
                .list();// 返回多条记录
        if (CollectionUtil.isNotEmpty(list)){
            for (Task task : list) {
                if (task.getProcessInstanceId().equals(procInstId)){
                    Map<String, Object> personMap = new HashMap<>();
                    personMap.put(nextZhiwei,nextAssignee);
                    personMap.put("value",value);
                    taskService.complete(task.getId(),personMap);
                }
            }
        }

    }
}
